[assembly: WebActivator.PreApplicationStartMethod(typeof(Legazpi.Payroll.Web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(Legazpi.Payroll.Web.App_Start.NinjectWebCommon), "Stop")]

namespace Legazpi.Payroll.Web.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
    using Legazpi.Payroll.BusinessLogic.Services;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }
        
        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }
        
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            
            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {            
            kernel.Bind<IClientService>().To<ClientService>().InRequestScope();
            kernel.Bind<IEmployeeService>().To<EmployeeService>().InRequestScope();
            kernel.Bind<IPhilHealthTableService>().To<PhilHealthTableService>().InRequestScope();
            kernel.Bind<ISSSTableService>().To<SSSTableService>().InRequestScope();
            kernel.Bind<IPositionService>().To<PositionService>().InRequestScope();
            kernel.Bind<IUserService>().To<UserService>().InRequestScope();
            kernel.Bind<IPayrollDateService>().To<PayrollDateService>().InRequestScope();
            kernel.Bind<IPharaphernaliaService>().To<PharaphernaliaService>().InRequestScope();
            kernel.Bind<IAdjustmentService>().To<AdjustmentService>().InRequestScope();
            kernel.Bind<ISettingsService>().To<SettingsService>().InRequestScope();
            kernel.Bind<IDayTimeRecordService>().To<DayTimeRecordService>().InRequestScope();
            kernel.Bind<ISectorService>().To<SectorService>().InRequestScope();
            kernel.Bind<IEmployeeAdjustmentService>().To<EmployeeAdjustmentService>().InRequestScope();
            kernel.Bind<IPayrollSummaryService>().To<PayrollSummaryService>().InRequestScope();
            kernel.Bind<IContributionService>().To<ContributionService>().InRequestScope();
            kernel.Bind<IGlobalAdjustmentService>().To<GlobalAdjustmentService>().InRequestScope();
            kernel.Bind<IDetachmentService>().To<DetachmentService>().InRequestScope();    
        }        
    }
}
